package com.luckyframe.project.jmeter.jmeterTestPlan.service;

import com.luckyframe.common.support.Convert;
import com.luckyframe.project.jmeter.jmeterTaskScheduling.domain.JmeterTaskScheduling;
import com.luckyframe.project.jmeter.jmeterTaskScheduling.service.IJmeterTaskSchedulingService;
import com.luckyframe.project.jmeter.jmeterTestPlan.domain.JmeterTestPlan;
import com.luckyframe.project.jmeter.jmeterTestPlan.mapper.JmeterTestPlanMapper;
import com.luckyframe.project.monitor.job.domain.Job;
import com.luckyframe.project.monitor.job.service.IJobService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * jmx测试计划 服务层实现
 * 
 * @author lancelot
 * @date 2020-09-30
 */
@Service
public class JmeterTestPlanServiceImpl implements IJmeterTestPlanService 
{
	@Autowired
	private JmeterTestPlanMapper jmeterTestPlanMapper;

	@Autowired
	private IJmeterTaskSchedulingService jmeterTaskSchedulingService;

	@Autowired
	private IJobService jobService;


	/**
     * 查询jmx测试计划信息
     * 
     * @param id jmx测试计划ID 主键ID
     * @return jmx测试计划信息 基础信息
     */
    @Override
	public JmeterTestPlan selectJmeterTestPlanById(Long id)
	{
	    return jmeterTestPlanMapper.selectJmeterTestPlanById(id);
	}
	
	/**
     * 查询jmx测试计划列表
     * 
     * @param jmeterTestPlan jmx测试计划信息 基础信息
     * @return jmx测试计划集合
     */
	@Override
	public List<JmeterTestPlan> selectJmeterTestPlanList(JmeterTestPlan jmeterTestPlan)
	{
	    return jmeterTestPlanMapper.selectJmeterTestPlanList(jmeterTestPlan);
	}
	
    /**
     * 新增jmx测试计划
     * 
     * @param jmeterTestPlan jmx测试计划信息 基础信息
     * @return 结果
     */
	@Override
	public int insertJmeterTestPlan(JmeterTestPlan jmeterTestPlan)
	{
	    return jmeterTestPlanMapper.insertJmeterTestPlan(jmeterTestPlan);
	}
	
	/**
     * 修改jmx测试计划
     * 
     * @param jmeterTestPlan jmx测试计划信息 基础信息
     * @return 结果
     */
	@Override
	public int updateJmeterTestPlan(JmeterTestPlan jmeterTestPlan)
	{
	    return jmeterTestPlanMapper.updateJmeterTestPlan(jmeterTestPlan);
	}

	/**
     * 删除jmx测试计划对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteJmeterTestPlanByIds(String ids)
	{
		return jmeterTestPlanMapper.deleteJmeterTestPlanByIds(Convert.toStrArray(ids));
	}

    @Override
    public List<JmeterTestPlan> selectByIdList(List<Long> ids) {
		if(CollectionUtils.isEmpty(ids)){
			return new ArrayList<>(0);
		}

		return jmeterTestPlanMapper.selectByIdList(ids);

	}

    @Override
    public Job getRelatedJobByPlanId(Long planId) {
		JmeterTaskScheduling scheduling = jmeterTaskSchedulingService.getByPlanId(planId);
		if(scheduling==null){
			return null;
		}
		return jobService.selectJobById(scheduling.getJobId());
	}

    @Override
    public void combineJobAndPlan(Long jobId, Long planId) {
		JmeterTaskScheduling scheduling = jmeterTaskSchedulingService.getByPlanId(planId);
		if(scheduling!=null){
			scheduling.setJobId(jobId);
			jmeterTaskSchedulingService.updateJmeterTaskScheduling(scheduling);
		}else {

			JmeterTaskScheduling newSche=new JmeterTaskScheduling();
			newSche.setJobId(jobId);
			newSche.setPlanId(planId);
			jmeterTaskSchedulingService.insertJmeterTaskScheduling(newSche);
		}

	}

}
